// pages/server_end/working_hours/working_hours.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    startTimeArray: [
      ['00', '01', '02', '03', '04', '05', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14',
        '15', '16', '17', '18', '19', '20', '21', '22', '23'
      ],
      ['00', '30'],
    ],
    startIndex: [0, 0],
    endTimeArray: [
      ['00', '01', '02', '03', '04', '05', '05', '06', '07', '08', '09', '10', '11', '12', '13', '14',
        '15', '16', '17', '18', '19', '20', '21', '22', '23'
      ],
      ['00', '30'],
    ],
    endIndex: [0, 0],
  },
  bindStartTimeChange: function(e) {
    console.log('修改的列为：' + e.detail.column + '，值为：' + e.detail.value)
    var data = {
      multiArray: this.data.startTimeArray,
      multiIndex: this.data.startIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0: //拖动第1列
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['00', '30']
            break
          case 1:
            data.multiArray[1] = ['00', '30']
            break
        }

        // this.startIndex.splice(1, 1, 0)
        // this.startIndex.splice(2, 1, 0)
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break;
    }

    // this.$forceUpdate()
  },
  bindStartChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      startIndex: e.detail.value
    })
  },
  bindEndTimeChange: function(e) {
    console.log('修改的列为：' + e.detail.column + '，值为：' + e.detail.value)
    var data = {
      multiArray: this.data.endTimeArray,
      multiIndex: this.data.endIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value
    switch (e.detail.column) {
      case 0: //拖动第1列
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['00', '30']
            break
          case 1:
            data.multiArray[1] = ['00', '30']
            break
        }
        // this.endIndex.splice(1, 1, 0)
        // this.endIndex.splice(2, 1, 0)
        data.multiIndex[1] = 0;
        data.multiIndex[2] = 0;
        break
    }

    // this.$forceUpdate()
  },
  bindEndChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endIndex: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          that.getWorkerInfo();
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  getWorkerInfo:function(){
    var that = this;
    let param = {
      scoure: 'wxapp',
      token:that.data.token
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.workerinfo, param, url.REQUEST_METHOD_GET).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      if(res.data.code==1){
         that.setData({
             userinfo : res.data.data,
             startIndex:res.data.data.work_start_time_text,
             endIndex:res.data.data.work_end_time_text
         })
       }
    })  	
  },
  updateTime(){
    var that = this;
    var work_start_time = that.data.startTimeArray[0][that.data.startIndex[0]]+':'+that.data.startTimeArray[1][that.data.startIndex[1]];
    var work_end_time = that.data.endTimeArray[0][that.data.endIndex[0]]+':'+that.data.endTimeArray[1][that.data.endIndex[1]];
    let param = {
      scoure: 'wxapp',
      token:that.data.token,
      work_start_time:work_start_time,
      work_end_time:work_end_time
    }
    wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
    request.request(url.setworktime, param, url.REQUEST_METHOD_POST).then((res) => {
      wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
      wx.showToast({
        title: res.data.msg,
        icon:"none"
      })
      if(res.data.code==1){
        setTimeout(function(){
          wx.navigateBack({
            delta:1
          })							
        },1500)
      }
    })  	
  }
})